package kz.pdt12.hw.framework;

import org.openqa.selenium.By;

/**
 * Created with IntelliJ IDEA.
 * User: raimbek
 * Date: 09.03.13
 * Time: 1:01
 */
public class NavigationHelper extends WebDriverHelperBase {
    public NavigationHelper(ApplicationManager manager) {
        super(manager);
    }

    public NavigationHelper mainPage() {
        if (! onMainPage()) {
            driver.findElement(By.linkText("home")).click();
        }
        return this;
    }

    private boolean onMainPage() {
        return driver.findElements(By.name("maintable")).size() > 0;
    }

    public GroupHelper groupsPage() {
        if (! onGroupsPage()) {
            driver.findElement(By.linkText("groups")).click();
        }
        return new GroupHelper(manager);
    }

    private boolean onGroupsPage() {
        if (driver.getCurrentUrl().contains("/group.php")
            && driver.findElements(By.name("new")).size() > 0) {
            return true;
        }
        return false;
    }

    public ContactHelper newContactPage() {
        if (! onNewContactPage()) {
            driver.findElement(By.linkText("add new")).click();
        }
        return new ContactHelper(manager);
    }

    private boolean onNewContactPage() {
        return driver.getCurrentUrl().contains("/edit.php");
    }

    public PrintPhonesHelper printPhones() {
        driver.findElement(By.linkText("print phones")).click();
        return new PrintPhonesHelper(manager);
    }

}
